
'use strict';

const Service = require('egg').Service;
class baseResultService extends Service {
  async successFun (data) {
    let obj = {
      status:'0',
      msg:'成功',
      result:{
        count:data.length,
        list:data
      }
    };
    return obj;
  }
  async errorFun(data) {
    let obj = {
      status:'1',
      msg:data,
      result: null
    };
    return obj;
  }
  async operateFunSuccess(){
    let obj = {
      status:'0',
      // result:data,
      msg:'成功',
    };
    return obj;
  }
  async operateFunError(){
    let obj = {
      status:'1',
      // result:data,
      msg:'失败',
    };
    return obj;
  }
  async isOperate(data){
    let obj = '';
    if(data.erros){
      obj = {
        status:'1',
        // result:data,
        msg:'失败',
      };
    }else {
      obj = {
        status:'0',
        msg:'成功',
      };
    }
    return obj;
  }
  async isLogin(data){

    let obj = '';
    if(data == null){
      obj = {
        status:'1',
        result:null,
        msg:'失败',
      };
    }else if(data.erros){
      obj = {
        status:'1',
        result:data,
        msg:'失败',
      };
    }else {

      const { ctx,app} = this;
      const token = app.jwt.sign({
        'userName': data.userName, //需要存储的 token 数据

      }, app.config.jwt.secret);
      ctx.set({'authorization':token})//设置headers
      obj = {
        status:'0',
        msg:'成功',
        result:{
          token:token
        },
      };
    }
    return obj;
  }
  async decryptFun() {
    let decode = '';
    const { ctx } = this;
    const token = ctx.request.header.authorization;
    let tokens = token.replace('Bearer ', '');
    // 解码token
    decode = ctx.app.jwt.verify(tokens, this.config.jwt.secret);
    return decode;
  }

}
module.exports = baseResultService;
